<template>
  <BasicModal
    v-bind="$attrs"
    destoryOnClose
    width="45%"
    @register="registerModal"
    :title="getTitle"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getRestoreNewEntryOptionsApi } from '/@/api/flow/flow';

  import { newConditionFormSchema } from './data';

  export default defineComponent({
    name: 'DeptModal',
    components: { BasicModal, BasicForm },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const { createMessage } = useMessage();
      let activityList;

      const [registerForm, { resetFields, validate, updateSchema }] = useForm({
        labelWidth: 100,
        baseColProps: { span: 24 },
        schemas: newConditionFormSchema,
        showActionButtonGroup: false,
      });

      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        resetFields();
        setModalProps({ confirmLoading: false });
        isUpdate.value = !!data?.isUpdate;

        if (data.record) {
          const res = await getRestoreNewEntryOptionsApi({ workItemID: data.record.workItemId });

          const { restoreNewEnrtyActivityResList, restoreNewEnrtyDetailResList } = res;

          activityList = restoreNewEnrtyActivityResList;

          const activityOptions = restoreNewEnrtyActivityResList.map((item) => ({
            label: item.activityName,
            value: item.activityId,
          }));

          const detailOptions = restoreNewEnrtyDetailResList.map((item) => ({
            label: item.showTitle,
            value: item.entryID,
          }));

          const schemasArr = [
            {
              field: 'activityId',
              componentProps: {
                options: activityOptions,
              },
            },
            {
              field: 'originalID',
              componentProps: {
                options: detailOptions,
              },
            },
          ];

          updateSchema(schemasArr);

          console.log('res', res);
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增状态' : '修改状态'));

      async function handleSubmit() {
        try {
          const values = await validate();
          setModalProps({ confirmLoading: true });

          console.log('values', values);

          const { addRecipientIdList, activityId } = values;

          const activityObj = activityList.find((item) => item.activityId == activityId);

          const sucVal = addRecipientIdList.map((item) => ({
            userId: item.userId,
            name: item.name,
          }));
          closeModal();
          createMessage.success('操作成功');
          emit('success', { ...values, addRecipientIdList: sucVal, ...activityObj });
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        registerForm,
        getTitle,
        handleSubmit,
      };
    },
  });
</script>
